Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tuple index out of range in custom schema tests #1903

Conversation

JusLarsen
Copy link

@JusLarsen JusLarsen commented Nov 8, 2019

This PR fixes #1808

We're looking into using dbt at work and I thought that helping with a small issue would be a great way to familiarize myself with the way it works. Apologies if I've missed a style guide or submitted something incorrectly! I'm happy to make any adjustments required.

Code Changes

  • modified node_runners.py. I added another condition under which a RuntimeErroris raised by the execute_test function. If table has zero rows, it provides a RuntimeError message instead of crashing.

Test Changes

All changes below are relative to the 008_schema_tests_test/ directory. I wasn't 100% sure where to put my tests but this seemed like the best place given the issue being addressed (macro failures in schema tests).

  • added macros-v2/malformed/tests.sql. This macro will always return an empty result set to trigger the conditions seen in the linked issue. I tried to follow the directory layout as seen in this and other tests by creating a malformed subdirectory.
  • moved macros-v2/tests.sql to macros-v2/macros/tests.sql so we can avoid loading the malformed macro for other previously existing tests.
  • added models-v2/custom-bad-test-macro/schema.yml based on the custom model. I trimmed the model down to have one test that will fail and one that will error.
  • added models-v2/custom-bad-test-macro/table_copy.sql based on the custom model.
  • modified test_schema_v2_tests.py. I added TestMalformedMacroTests for testing schemas with malformed macros and I updated TestHooksInTests to point to the original macro file now nested one directory further down.

Verification of fix

Remove the new lines from node_runners.py and run tests. You'll see a hard crash with a tuple index out of range error.

Final thoughts

If you'd prefer I create an entirely new test folder or want me to move my tests just let me know. If this PR isn't up to snuff and you have the time for it, I always like feedback. I really dig this project and appreciate all the time and effort that has gone into it. :)

@cla-bot
Copy link

cla-bot bot commented Nov 8, 2019

Thanks for your pull request, and welcome to our community! We require contributors to sign our Contributor License Agreement and we don't seem to have your signature on file. Check out this article for more information on why we have a CLA.

In order for us to review and merge your code, please submit the Individual Contributor License Agreement form attached above above. If you have questions about the CLA, or if you believe you've received this message in error, don't hesitate to ping @drewbanin.

CLA has not been signed by users: @JusLarsen

@JusLarsen JusLarsen changed the title Fix tuple index out of range in custom schema tests (#1808) Fix tuple index out of range in custom schema tests Nov 8, 2019
Copy link
Contributor

@drewbanin drewbanin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sending through this PR and for a very thoughtful writeup! I think you did a great job with the tests and this is all looking really good to me :)

I did add one comment - let me know what you think about that. I'm going to kick off the integration tests presently. Looking forward to getting this merged - I think we can sneak it in for our 0.15.0 release :)

@cla-bot check

@@ -551,6 +551,11 @@ def execute_test(self, test):
raise RuntimeError(
"Bad test {name}: Returned {rows} rows and {cols} cols"
.format(name=test.name, rows=num_rows, cols=num_cols))
elif num_rows == 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about changing the if statement above this line to if num_rows != 1?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially changed the if statement to use that exact logic, but ended up with the elif so that we could communicate a different RuntimeError message back to the user.

If you think the first error message is informative enough I'm happy to make that change quickly!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - I think that calling out the "shape" of the query results here makes sense. I suppose we can update this message to says "Bad test {name}: returned {rows} rows and {cols} cols but expected 1 row and 1 column". You buy that?

We use pep-8 as a style guide for dbt -- just make sure the log line isn't longer than 79 characters and you should be good!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me, I've updated the logic, rebased, and force pushed over the branch for this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great!

You did a really amazing job with this PR - it was a pleasure to review :D

Going to merge this when the tests are passing - it will ship in 0.15.0 in a couple of weeks

@drewbanin
Copy link
Contributor

turns out the @cla-bot check thing only works in comments - not reviews!

@cla-bot
Copy link

cla-bot bot commented Nov 8, 2019

The cla-bot has been summoned, and re-checked this pull request!

@cla-bot cla-bot bot added the cla:yes label Nov 8, 2019
@JusLarsen
Copy link
Author

Argh, I'm not sure why unit tests are failing in the pipeline, they're working fine for me using docker on mac os.

@drewbanin
Copy link
Contributor

Alright! This is ready to go as-is. If you don't mind though, I think we can make one more tiny change here that will improve the output of this exception. Instead of raising a RuntimeError, we can raise a compiler error - this will help dbt's error handling describe where the error occurred.

If you change the RuntimeError to instead use:

dbt.exceptions.raise_compiler_error(msg, test)

Then the error will look like:
Screen Shot 2019-11-09 at 7 31 39 PM

Instead of
Screen Shot 2019-11-09 at 7 33 17 PM

You can also use test.test_metadata.name instead of test.name to find the name of the test definition instead of the dbt-generated test name (eg. bad vs. bad_debug_id) as shown in the error message above.

@JusLarsen
Copy link
Author

JusLarsen commented Nov 11, 2019

Ooooh, I like the idea of swapping it to a compiler error.

I should have some time this afternoon to play around with the suggested change, I’ll reply in this thread once I’ve made the modification.

@JusLarsen
Copy link
Author

@drewbanin updated to use a compiler error instead, rebased, and pushed to my repo. We should be good to go now!

@drewbanin
Copy link
Contributor

Awesome - I just kicked off the tests here again. It looks like GitHub is showing the wrong checks - that happens sometimes. I'll merge this when they all pass :)

Thanks for your contribution to dbt!

@beckjake beckjake merged commit 9be47e6 into dbt-labs:dev/louisa-may-alcott Nov 13, 2019
@beckjake
Copy link
Contributor

Thanks for the awesome PRs @JusLarsen, this is great - I'm so happy this is finally going to be a good error message!

@JusLarsen JusLarsen deleted the fix/schema-test-error branch November 13, 2019 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tuple index out of range in custom schema tests with select *
3 participants